<?php
declare(strict_types=1);
/**
 * MineAdmin is committed to providing solutions for quickly building web applications
 * Please view the LICENSE file that was distributed with this source code,
 * For the full copyright and license information.
 * Thank you very much for using MineAdmin.
 *
 * @Author X.Mo<root@imoi.cn>
 * @Link   https://gitee.com/xmo/MineAdmin
 */

namespace Plugin\Device\Service;

use App\Exception\BusinessException;
use App\Http\Common\ResultCode;
use Common\Common;
use Plugin\Device\Repository\RdDeviceRodRepository;
use Plugin\Device\Repository\RdDeviceSerialRepository;
use App\Service\IService;
use Plugin\EasyCVR\Service\EasyCVRService;

/**
 * 录像机设备服务类
 */
final class RdDeviceSerialService extends IService
{
    public function __construct(
        protected readonly RdDeviceSerialRepository $repository,
        protected readonly RdDeviceRodRepository $rodRepository
    ) {}

    public function page(array $params, int $page = 1, int $pageSize = 10): array
    {
        $data = $this->repository->page($params, $page, $pageSize);
        foreach ($data['list'] as &$item) {
            if (!$item['online'] == 1) {
                continue;
            }
            try {
                $res = Common::container()->get(EasyCVRService::class)->channelStream(
                    strval($item['device_serial']),
                    strval($item['channel_no']),
                    $params['protocol'] ?? 'ws_flv',
                    $params['stream'] ?? 'main'
                );
                $item['snap'] = $res['SnapURL'];
            } catch (\Throwable $e) {

            }
        }
        return $data;
    }

    public function create(array $data): mixed
    {
        $rodId = $data['rod_id'];
        $rod = $this->rodRepository->findById($rodId);
        if (!$rod) {
            throw new BusinessException(ResultCode::NOT_FOUND, '装置不存在');
        }
        $data['stand_id'] = $rod->stand_id;
        return parent::create($data);
    }

    public function ptz(int $id, array $all)
    {
        $camera = $this->repository->findById($id);
        if (!$camera) {
            throw new BusinessException(ResultCode::NOT_FOUND, '设备不存在');
        }
        Common::container()->get(EasyCVRService::class)->PTZControl(
            strval($camera->device_serial),
            strval($camera->channel_no),
            $all['command'],
            $all['direction'] ?? 'up',
            intval($all['speed'] ?? 50)
        );
    }

    public function getPlayAddr(int $id, array $all)
    {
        $camera = $this->repository->findById($id);
        if (!$camera) {
            throw new BusinessException(ResultCode::NOT_FOUND, '设备不存在');
        }
        return Common::container()->get(EasyCVRService::class)->channelStream(
            strval($camera->device_serial),
            strval($camera->channel_no),
            $all['protocol'] ?? 'fmp4',
            $all['stream'] ?? 'main'
        );
    }

    public function detail(int $id)
    {
        return $this->repository->findById($id);
    }
}